package com.bistu.xiong.common.app;


import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bistu.xiong.common.R;
import com.bistu.xiong.common.widget.convention.PlaceHolderView;

import butterknife.ButterKnife;
import butterknife.Unbinder;


public abstract class Fragment extends android.support.v4.app.Fragment {
    protected View mRoot;
    protected Unbinder mRootUnBinder;
    protected PlaceHolderView mPlaceHolderView;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        //初始化参数
        initArgs(getArguments());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        //假如第一次创建
        if(mRoot==null){
            int layId = getContentLayoutId();
            //初始化当前的根布局，但是不在创建时就添加到container里边
            View root = inflater.inflate(layId,container,false);
            initWidget(root);
            initData();
            mRoot = root;
        }
        //假如创建过View，则直接复用
        else{
            if(mRoot.getParent()!=null){
                //把当前Root从其父控件中移除
                ((ViewGroup)mRoot.getParent()).removeView(mRoot);
            }
        }

        return mRoot;
    }
    protected void initWidget(View root){
        //ButterKnife初始化
        mRootUnBinder = ButterKnife.bind(this,root);
    }
    protected void initData(){

    }
    /**
     * 给布局id
     * @return
     */
    protected abstract int getContentLayoutId();
    /**
     * 初始化相关参数
     */
    protected void initArgs(Bundle bundle){

    }
    
    /**
     * 设置占位布局
     * @param placeHolderView 继承了占位布局规范的View
     */
    public void setPlaceHolderView(PlaceHolderView placeHolderView) {
        this.mPlaceHolderView = placeHolderView;
    }
}
